home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10322 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  41 lines

  1. Path: bmtlh10.bnr.ca!news
  2. From: John Hickin <hickin@bnr.ca>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: () syntax for constructor for an array?
  5. Date: 7 Mar 1996 14:20:55 GMT
  6. Organization: Bell Northern Research
  7. Message-ID: <4hmrc7$1dc@bmtlh10.bnr.ca>
  8. References: <NICKC.96Mar7121149@uxe.liv.ac.uk>
  9. NNTP-Posting-Host: bmtlh520.bnr.ca
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1 (X11; I; HP-UX A.09.05 9000/715)
  14. X-URL: news:NICKC.96Mar7121149@uxe.liv.ac.uk
  15.  
  16. There doesn't seem a useful way in C++ to initialize an array of objects
  17. unless the initial values are all the same.  When this restriction does
  18. hold, however, you have to ensure that the default constructor passes in
  19. the right value.  
  20.  
  21. These days you might be able to use a template to ease the pain of setting
  22. up more general default values:
  23.  
  24.    struct B { B(int = 42); B(const char*, float); };
  25.    
  26.    template < int InitialValue >
  27.    struct B1 : public B { B1() : B(InitialValue) {} };
  28.  
  29.    template < const char* Id , float F >
  30.    struct B2 : public B { B2() : B(Id,F) {} };
  31.  
  32. I can't be sure because the above template feature isn't supported by any
  33. compiler I have access to.  A code generator I had the pleasure of working
  34. on, however, employed this "derive a class for initialization" trick.  I
  35. found that it tended to generate a lot of code.
  36.  
  37. -- 
  38. John Hickin      Nortel Technology, Montreal, Quebec
  39. (514) 765-7924   hickin@bnr.ca
  40.  
  41.